#!/bin/bash

### BEGIN INIT INFO
# Provides: hzvault 
# Required-Start:
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: Hzvault 
# Description: Start Hzvault
### END INIT INFO 
TRUE=1
FALSE=0
case "$1" in
    start)

USPLASH=$FALSE

if [ -f /sbin/usplash_write ]; then
    # use innocuous command to determine if usplash is running
    # usplash_write will return exit-code 1 if usplash isn't running
    # need to set a flag to tell usplash_write to report no usplash
    FAIL_NO_USPLASH=1
    #enable verbose messages (required to display messages if kernel boot option "quiet" is enabled
    /sbin/usplash_write "VERBOSE on"
    if [ $? -eq $TRUE ]; then
        # usplash is running
        USPLASH=$TRUE
        /sbin/usplash_write "CLEAR"
    fi
fi

msg ()
{
    if [ $# -gt 0 ]; then
        # handle multi-line messages
        echo $2 | while read LINE; do
            if [ $USPLASH -eq $TRUE ]; then
                # use usplash
                /sbin/usplash_write "$1 $LINE"
            else
                # use stderr for all messages
                echo $3 "$2" >&2
            fi
        done
    fi
}


	msg CLEAR
        msg TEXT "Checking BOOTloader integrity"
	DEVBOOT=`mount |grep boot |awk '{print $1}'`
	dd if=$DEVBOOT of=/etc/hzvault/mbr-hzvault bs=512 count=1

function confirm {
                msg TEXT "$1"
		read -r line </dev/console >/dev/null
        while [ "$line" != "YES" ] && [ "$line" != "NO" ]; do
               	msg TEXT "Not good answer"
		msg TEXT "$1"
                read line
        done
		}


	if ! sha1sum -c /etc/hzvault/hzvault-bootloader.sum > /dev/null ; then
 
		msg TEXT "CAUTION Bootloader modified"
		msg TEXT "Destroy your password & restore your bootloader"

		if confirm "Do you want halt your system (YES or NO)? (Type uppercase answer)"; then
			if test "$line" = "YES" ; then
        		        msg TEXT "shutdown"
                		/sbin/halt
			else
				msg TEXT "caution you're booting on a vulnerable system"
	                        msg TEXT "Do not enter password for your Documents"
	                        sleep 5
			fi	
		fi	

	fi	
	msg CLEAR
        msg TEXT "HZVAULT make subdirectories TMPFS"
        # /var/log subdirectories.
        mkdir -p /var/log/fsck
        mkdir -p /var/log/ksymoops
        mkdir -p /var/log/news

        # smbd
        if [ -f /usr/sbin/smbd ] ; then
            mkdir -p /var/log/samba
        fi

        # Apt archive subdirectories.
        mkdir -p /var/cache/apt/archives/partial
        msg TEXT "TMPFS hzvault done."
	DOCUUID=`echo AAAAA`
	msg CLEAR
	msg TEXT "Mount Documents Partition, Enter password :"
	echo debut

	while true; do
	cryptsetup -t 10 -T 1 luksOpen /dev/disk/by-uuid/$DOCUUID data
	result=$?
	if [ $result -eq 234 ] || [ $result -eq 0 ]; then
		break
	else
		msg CLEAR
		msg TEXT "password incorrect"
		msg TEXT "Mount Documents Partition, Enter password :"
	fi
	done
	mount /dev/mapper/data /home/data
        echo
	if [ -f /etc/init.d/hzvirt.sh ]
	then
		/etc/init.d/hzvirt.sh start
	fi
        ;;
    stop)
        msg TEXT "HZVAULT shutting down."
        # Add anything that needs to happen at shutdown here.
        echo
        ;;
    *)
        echo "Usage: hzvault {start|stop}"
        exit 1
        ;;
esac

